package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.wordmemorize.converter.AdminLogConverter;
import com.cskaoyan.wordmemorize.dao.entity.OperateLogDO;
import com.cskaoyan.wordmemorize.dao.mapper.OperateLogMapper;
import com.cskaoyan.wordmemorize.dto.admin.OperateLogDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.OperateLogPageRequest;
import com.cskaoyan.wordmemorize.service.AdminLogService;
import org.apache.ibatis.executor.BatchExecutorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 顾海龙
 * @version 1.0
 */

@Service
public class AdminLogServiceImpl extends ServiceImpl<OperateLogMapper, OperateLogDO> implements AdminLogService {

    @Autowired
    AdminLogConverter adminLogConverter;

    @Autowired
    OperateLogMapper operateLogMapper;

    //查询系统操作日志
    @Override
    public PageDTO<OperateLogDTO> getOperateLogPage(OperateLogPageRequest request) throws BatchExecutorException {
        PageDTO<OperateLogDTO> logDTOPageDTO;
        QueryWrapper<OperateLogDO> operateLogDOQueryWrapper = new QueryWrapper<>();
        if (request.getEmployeeId() != null && !("".equals(request.getEmployeeId()))) {
            operateLogDOQueryWrapper.eq("employee_id", request.getEmployeeId());
        }
        if (request.getDataType() != null && request.getDataType().length() != 0) {
            operateLogDOQueryWrapper.eq("data_type",request.getDataType());
        }
        if (request.getKeyword() != null && !("".equals(request.getKeyword()))){
            operateLogDOQueryWrapper.like("data",request.getKeyword());
        }
        if (request.getPageNum() == null || request.getPageNum() < 0) {
            List<OperateLogDO> list = this.list(operateLogDOQueryWrapper);
            logDTOPageDTO = adminLogConverter.operationLogPage2PageDTO(list,(long)list.size());
        }
        else {
            Page<OperateLogDO> operateLogDOPage = new Page<>(request.getPageNum(),request.getCount());
            Page<OperateLogDO> operateLogDOPageList = operateLogMapper.selectPage(operateLogDOPage, operateLogDOQueryWrapper);
            logDTOPageDTO = adminLogConverter.operationLogPage2PageDTO(operateLogDOPageList.getRecords(),operateLogDOPageList.getTotal());
        }
        return logDTOPageDTO;
    }
}
